<template>
  <!-- 搜索区域 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="90px"
    >
      <el-form-item label="三方应用" prop="socialClientId">
        <el-select
          v-model="queryParams.socialClientId"
          placeholder="请选择客户端编号"
          class="!w-240px"
        >
          <el-option
            v-for="client in socialClientList"
            :key="client.id"
            :value="client.id"
            :label="client.name"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="社交平台" prop="type">-->
      <!--        <el-select-->
      <!--          v-model="queryParams.type"-->
      <!--          placeholder="请选择社交平台"-->
      <!--          clearable-->
      <!--          class="!w-240px"-->
      <!--        >-->
      <!--          <el-option label="请选择字典生成" value="" />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="openid" prop="openid">
        <el-input
          v-model="queryParams.openid"
          placeholder="请输入openid"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表区域 -->
  <ContentWrap>
    <el-table v-loading="listLoading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" prop="id" width="100" />
      <el-table-column label="三方应用" prop="socialClientId">
        <template #default="scope">
          {{ socialClientList.find((client) => client.id === scope.row.socialClientId)?.name }}
        </template>
      </el-table-column>
      <el-table-column label="社交平台" prop="type">
        <template #default="scope">
          <dict-tag :options="common_social_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="openid" prop="openid" width="350" />
      <el-table-column label="用户昵称" prop="nickname" />
      <el-table-column label="用户头像" prop="avatar">
        <template #default="{ row }">
          <el-image v-if="row.avatar" :src="row.avatar" class="h-30px w-30px" @click="imagePreview(row.avatar)" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" width="180px" />
      <el-table-column label="操作" width="100px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openDetail(scope.row)"
            v-hasPermi="['system:social-user:query']"
          >
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：详情 -->
  <SocialUserDetail ref="detailRef" />
</template>

<script setup lang="ts">
import { SocialUserApi, SocialUserVO } from '@/api/system/social/social_user'
import SocialUserDetail from './SocialUserDetail.vue'
import { SocialClientApi, SocialClientVO } from '@/api/system/social/social_client'
import { createImageViewer } from '@/components/ImageViewer'

/** 社交用户 列表 */
defineOptions({ name: 'SystemSocialUser' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { common_social_type } = useIntDict(DICT_TYPE.common_social_type)

const listLoading = ref(true) // 列表的加载中状态
const list = ref<SocialUserVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  type: undefined,
  socialClientId: undefined as any,
  openid: undefined,
  nickname: undefined,
  createTime: [],
  pageNo: 1,
  pageSize: 10
})
const queryFormRef = ref() // 搜索的表单
const socialClientList = ref<SocialClientVO[]>([])

/** 查询列表 */
const getList = async () => {
  listLoading.value = true
  try {
    const data = await SocialUserApi.getSocialUserPage(queryParams)
    list.value = data.content
    total.value = data.totalElement
  } finally {
    listLoading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const detailRef = ref<InstanceType<typeof SocialUserDetail>>()
const openDetail = (data: SocialUserVO) => {
  detailRef.value?.open(data)
}

const imagePreview = (imgUrl: string) => {
  createImageViewer({
    urlList: [imgUrl]
  })
}

/** 初始化 **/
onMounted(async () => {
  socialClientList.value = await SocialClientApi.getSocialClientSimpleList()
  if (socialClientList.value?.length) {
    queryParams.socialClientId = socialClientList.value[0].id
  }
  await getList()
})
</script>

<style scoped lang="scss"></style>
